package cn.net.xyan.easy.gwt.client.ui;

import cn.net.xyan.easy.gwt.client.event.ActionEvent;
import cn.net.xyan.easy.gwt.client.event.ActionEventBus;
import cn.net.xyan.easy.gwt.client.event.ActionHandler;
import cn.net.xyan.easy.gwt.client.model.MenuItemModel;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import org.gwtbootstrap3.client.ui.AnchorButton;
import org.gwtbootstrap3.client.ui.Icon;
import org.gwtbootstrap3.client.ui.NavPills;
import org.gwtbootstrap3.client.ui.base.AbstractListItem;
import org.gwtbootstrap3.client.ui.base.button.AbstractToggleButton;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.client.ui.constants.Styles;

/**
 * Created by zarra on 16/2/17.
 */
public class Menu extends AbstractListItem {

    MenuItemModel model;
    AnchorButton button;

    Icon buttonIcon;

    NavPills pills;
    boolean showMenu = false;

    public Menu(final MenuItemModel model) {
        assert model!=null;
        this.model = model;
        setStyleName(Styles.DROPDOWN);



        button = new AnchorButton();
        String icon = model.getIcon();
        if (icon == null || icon.length() ==0){
            button.setIcon(IconType.COGS);
        }else {
            button.setIcon(model.icon());
        }


        InlineLabel buttonInlineLabel = new InlineLabel(model.getTitle());
        buttonInlineLabel.getElement().getStyle().setMarginLeft(5, Style.Unit.PX);
        button.add(buttonInlineLabel);

        if (!model.isNode()){
            buttonIcon = new Icon(IconType.ANGLE_LEFT);
            buttonIcon.getElement().getStyle().setMarginRight(10, Style.Unit.PX);
            buttonIcon.getElement().getStyle().setFloat(Style.Float.RIGHT);
            button.add(buttonIcon);
        }

        button.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (model.isNode()){
                    ActionEvent.fire(ActionEventBus.eventBus(),Menu.this,model);
                }else {
                    showMenu = !showMenu;
                    UIObject.setVisible(pills.getElement(), showMenu);
                    if (showMenu){
                        buttonIcon.setType(IconType.ANGLE_DOWN);
                    }else{
                        buttonIcon.setType(IconType.ANGLE_LEFT);
                    }
                }
            }
        });

        internalAdd(button);

        pills = new NavPills();
        pills.setStacked(true);
        UIObject.setVisible(pills.getElement(), showMenu);

        if (!model.isNode()){
            for (MenuItemModel menuItemModel : model.getItems()){
                MenuItem menuItem = new MenuItem(menuItemModel);
                add(menuItem);

                menuItem.getElement().getStyle().setHeight(30, Style.Unit.PX);
            }
        }

        internalAdd(pills);


    }


    private void internalAdd(final Widget child) {
        if (child instanceof AbstractToggleButton) {
            if (!(child instanceof AnchorButton)) {
                throw new IllegalArgumentException("Only buttons of type AnchorButton can be added to ListDropDown");
            }

            child.setStyleName(Styles.DROPDOWN_TOGGLE);
        }

        add(child, (Element) getElement());
    }


    @Override
    public void add(final Widget child) {
        if (pills!=null){
            //child.getElement().getStyle().setMarginLeft(20, Style.Unit.PX);
            pills.add(child);
        }
    }

    public  HandlerRegistration addActionEventHandler(ActionHandler<MenuItemModel> handler){
        return addHandler(handler, ActionEvent.getType(MenuItemModel.class));
    }
}
